<template>
  <div class="subjectAdd">
    <el-dialog :visible.sync="isShow">
      <div class="title" slot="title">{{type}}</div>
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item prop="rid" label="学科编号">
          <el-input v-model="form.rid"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="学科名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="学科简称">
          <el-input v-model="form.short_name"></el-input>
        </el-form-item>
        <el-form-item label="学科简介">
          <el-input v-model="form.intro"></el-input>
        </el-form-item>
        <el-form-item label="学科备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button @click="isShow=false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addSubject, editSubject } from "@/api/subject.js";
export default {
  props: ["type", "mode"],
  watch: {
    isShow(newVal) {
      if (newVal === false) {
        this.form = {
          rid: "", //	是	string	学科编号
          name: "", //	是	string	学科名称
          short_name: "", //	否	string	学科简称
          intro: "", //	否	string	学科简介
          remark: "" //	否	string	备注
        };
        this.$nextTick(() => {
          this.$refs.form.clearValidate();
        });
      }
    }
  },
  data() {
    return {
      isShow: false,
      form: {
        rid: "", //	是	string	学科编号
        name: "", //	是	string	学科名称
        short_name: "", //	否	string	学科简称
        intro: "", //	否	string	学科简介
        remark: "" //	否	string	备注
      },
      rules: {
        rid: [{ required: true, message: "必填", trigger: "change" }],
        name: [{ required: true, message: "必填", trigger: "change" }]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(result => {
        if (result) {
          if (this.mode === "edit") {
            editSubject(this.form).then(() => {
              this.$message.success("编辑成功!");
              this.isShow = false;
              this.$emit("sonEdit");
            });
          } else {
            addSubject(this.form).then(() => {
              this.$message.success("新增成功!");
              this.isShow = false;
              this.$parent.search();
            });
          }
        } else {
          this.$message.error("验证失败");
        }
      });
    }
  }
};
</script>
<style lang="less">
.subjectAdd {
  .el-dialog__header {
    padding: 0px;
  }
  .el-icon-close:before {
    color: #fff;
  }
  .title {
    background: linear-gradient(to right, #01c4fa, #1395fa);
    text-align: center;
    height: 54px;
    line-height: 54px;
    color: #fff;
  }
  .footer {
    text-align: center;
  }
}
</style>